<template>
    <div class="timing">
      <!-- 按钮 -->
      <el-row style="margin-bottom:10px;">
        <el-col :span="24">
          <el-button type="primary" @click="addRole"><Icon type="md-add" />新增角色</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <admincentertb ref="httbRef" :btn-is-show="btnIsShow" :table-col="tabUserList" :table-data="tableUserData"
      @getDataDetileInfo="getDataDetileInfo"
       @deleteSelectData="deleteSelectData" @refresh="getUserList" />
      <!-- 分页 -->
      <div style="text-align:right;margin-top:10px;">
        <el-pagination
          :current-page="page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
  
      <el-dialog :close-on-click-modal="false" :title="title" width="30%" :visible.sync="dialogTableVisible">
        <!-- 名称和路径必输 -->
        <el-form v-if="dialogTableVisible" ref="ruleForm" :model="ruleForm" :rules="rules" label-width="80px" class="demo-ruleForm autoClass">
          <el-form-item label="角色名称" prop="roleName">
            <el-input :disabled="disFlag" v-model="ruleForm['roleName']" clearable placeholder="" />
          </el-form-item>
        </el-form>
        <div style="text-align: center;">
          <el-button type="primary" @click="commitFromData('ruleForm')">确定</el-button>
          <el-button @click="resetForm('return')">返回</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { usergetRoleList,useraddRole, userremoveRoleById,userupdateRoleName} from '@/api/home_/adminCenter'
  export default {
    components: {},
    data() {
      return {
        btnIsShow: {
          operate: true,
          detile: false,
          del: true
        },
        title: '新增角色',
        rules: {
          roleName:[
            { required: true, message: '角色名称不能为空', trigger: 'change' },
          ]
        },
        dialogTableVisible: false,
        ruleForm: {
          roleName:""
        },
        addParms: [],
        addParms2: [],
        tableUserData: [],
        fullscreenLoading:false,
        tabUserList: [
          {
            key: 'roleName',
            title: '角色名称'
          },
          {
            key: 'createTime',
            title: '创建时间'
          }
        ],
        total: 0,
        size: 10,
        changeList:[],
        page: 1
      }
    },
    created() {
  
    },
    mounted() {
      this.$nextTick(() => {
        this.getUserList()
      })
    },
    methods: { 
        resetForm(){
          this.dialogTableVisible = false
        },
        addRole(){
          this.title = '新增'
          this.ruleForm['roleName'] = ""
          this.dialogTableVisible = true
        },
        editRowData(v){
          this.title = '修改'
          this.dialogTableVisible = true
          this.ruleForm.roleName = JSON.parse(v).roleName
          this.ruleForm.roleId = JSON.parse(v).roleId
        },
        deleteSelectData(v) {
            userremoveRoleById(v.roleId).then(res => {
            if (res.code == 200) {
                this.$message.success(res.msg)
                this.getUserList()
            } else {
                this.errorDialog(res.msg)
            }
            })
        },
        handleNodeClick(data) {
            console.log(data)
        },
        commitFromData(formName) {
          this.$refs[formName].validate((valid) => {
            if (valid) {
              if(this.title=='新增'){
                delete this.ruleForm.roleId
                useraddRole(this.ruleForm).then(res => {
                    if (res.code == 200) {
                      this.$message.success('角色新增成功')
                       this.getUserList()
                    } else {
                        this.errorDialog(res.msg)
                    }
                })
                }else{
                userupdateRoleName(this.ruleForm).then(res => {
                    if (res.code == 200) {
                        this.$message.success('角色修改成功')
                        this.getUserList()
                    } else {
                        this.errorDialog(res.msg)
                    }
                })
              }
            } else {
              return false
            }
          })
           
        
        },
        handleSizeChange(v) {
          this.page = 1
            this.size = v
            this.getUserList()
        },
        handleCurrentChange(v) {
            this.page = v
            this.getUserList()
        },
        // 获取用户列表
        getUserList() {
            this.dialogTableVisible = false
            this.$refs.httbRef.isShowLoading(true)
            usergetRoleList({
                'page': this.page,
                'limit': this.size
            }).then(res => {
                this.tableUserData = res.data.roleList
                this.total = Number(res.data.total)
                this.$refs.httbRef.isShowLoading(false)
                this.loading= false
            })
        }
        }
    }
  </script>
  
  <style lang="scss" scoped>
    @import "~@/assets/styles/auto.scss";
        .info{
          margin-top: 20px;
        }
        .info>p{
          padding:4px 0;
          margin-left:80px;
          display: flex;
          flex-direction: row;
          justify-content: space-between;
        }
        .info>p:nth-child(1){
          color: #474747;
          font-weight: bold;
          background: #f1f4f7;
          padding-left: 0px;
        }
        .info>p>label{
          display: inline-block;
          width:35%;
        }
        .info>p>span{
          color:#494949;padding: 2px 4px;
          border-radius: 2px;font-size: 12px;
          background: #f0f0f0;
        }
      .ztree {
          margin-left:80px;
          border:1px solid #efefef;
          padding: 4px;
          border-radius: 2px;
          min-height: 270px;
          height: 300px;
          overflow-y: scroll;
      }
      .resoceDialog{
        display: flex;
        flex-direction: row;
        flex-wrap: nowrap;
        justify-content: space-between;
      }
      .resoceDialog>div>p{
        color: #464646;
        padding: 10px 0;
        font-weight: bold;
        // background: #f1f4f7;
        margin-left:26px;
        padding-left:0px;
        margin-bottom: 5px;
        text-align: right;
      }
        >>>.jsoneditor-poweredBy {
          display: none !important;
        }
  
        .addPs{
          display: flex;
          flex-direction: row;
          margin-bottom: 10px;
          >div{
            width:50%
          }
          span{
            display: inline-block;
            width: 10%;
          }
        }
        .autoClass{
          // display: flex;
          // flex-direction: row;
          // flex-wrap: wrap;
        }
        .autoClass .el-form-item{
          // width:50%;
        }
        .addcs{
          display: flex;
          flex-direction: row;
          margin-bottom: 10px;
        }
        .addcs2 .el-select {
          display: inline-block;
          width: 240px;
        }
        .autoInput{
          display: inline-block;
          width: 70%;
        }
        .infoTitle{
          border-bottom:1px solid #e5e5e5;
          margin-bottom: 30px;
          position: relative;
        }
        .infoTitle>span{
          position: absolute;
          left: 20px;
          background: #fff;
          width:80px;
          top:-15px;
          height:30px;
          text-align: center;
          line-height: 30px;
        }
          .el-select{
             width:100%;
          }
          .timing{
            background:#fff;
            padding:20px;
          }
        </style>
  